﻿using MediatR;
using NPOI.SS.Formula.Functions;
using ProcurementManagement.Applcation.Command.ProcureApplyCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Infrstructre.Interface;
using SqlSugar;

namespace ProcurementManagement.Applcation.CommandHanlers.ProcureApplyCommandHandler
{
    /// <summary>
    /// 显示采购申请中介者
    /// </summary>
    public class ProcureApplySelectCommandHandler : IRequestHandler<ProcureApplySelectCommand, ProcureApplyPage>
    {
        private readonly lnterfaceProcureApplyRepository _procureApplyRepository;//采购申请表
        private readonly lnterfacePurchaseproductRepository _purchaseproductRepository;//采购明细申请表
        private readonly ISqlSugarClient db;

        public ProcureApplySelectCommandHandler(lnterfaceProcureApplyRepository procureApplyRepository, lnterfacePurchaseproductRepository purchaseproductRepository, ISqlSugarClient db)
        {
            _procureApplyRepository = procureApplyRepository;
            _purchaseproductRepository = purchaseproductRepository;
            this.db = db;
        }

        public async Task<ProcureApplyPage> Handle(ProcureApplySelectCommand request, CancellationToken cancellationToken)
        {
            ProcureApplyPage page = new ProcureApplyPage();

            var res = db.Queryable<ProcureApply>()
                .LeftJoin<Purchaseproduct>((pa, pp) => pa.PANumbers == pp.PANumber)
                .Where(pa=>pa.IsDel==false)
                .GroupBy(pa => new { pa.PANumbers,pa.ProcureApplyId,pa.PANumber,pa.PAApplyMotif,pa.PAApplicationType ,pa.PAEnquiryState, pa.PAPurchaseState,pa.Dates,pa.AddName, pa.IsDel})
                .Select((pa, pp) => new ProcureApplySelectDto
                {
                    ProcureApplyId = pa.ProcureApplyId,
                    PANumber = pa.PANumber,
                    PAApplyMotif = pa.PAApplyMotif,
                    PAApplicationType = pa.PAApplicationType,
                    SumPrice = SqlFunc.AggregateSum(pp.SumPrice),
                    PAEnquiryState = pa.PAEnquiryState,
                    PAPurchaseState = pa.PAPurchaseState,
                    Dates = pa.Dates,
                    AddName = pa.AddName,
                    IsDel = pa.IsDel
                }).ToList();
            if (!string.IsNullOrEmpty(request.PAApplyMotif))
            {
                res = res.Where(x => x.PAApplyMotif.Contains(request.PAApplyMotif)).ToList();
            }
            if (!string.IsNullOrEmpty(request.PANumber))
            {
                res = res.Where(x => x.PANumber == request.PANumber).ToList();
            }
            if (!string.IsNullOrEmpty(request.Kdate))
            {
                res = res.Where(x => x.Dates >= Convert.ToDateTime(request.Kdate)).ToList();
            }
            if (!string.IsNullOrEmpty(request.Sdate))
            {
                res = res.Where(x => x.Dates <= Convert.ToDateTime(request.Sdate)).ToList();
            }
            if (!string.IsNullOrEmpty(request.PAApplicationType))
            {
                res = res.Where(x => x.PAApplicationType == request.PAApplicationType).ToList();
            }
            if (request.SumPrice != null)
            {
                res = res.Where(x => x.SumPrice == request.SumPrice).ToList();
            }
            if (request.PAEnquiryState != null)
            {
                res = res.Where(x => x.PAEnquiryState == request.PAEnquiryState).ToList();
            }
            page.SumCount = res.Count();
            page.PageLast = (int)Math.Ceiling(page.SumCount * 1.0 / request.PageSize);
            // 5. 分页：按 PANumber 分组后的数据分页
            page.list = res.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;
        }
    }
}
